/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.common.StringUtil;
import com.ntb.controllers.forms.UserForm;
import com.ntb.entities.Users;
import com.ntb.models.UserGroupsDAO;
import com.ntb.models.UsersDAO;
import com.ntb.models.impl.UserGroupsDAOImpl;
import com.ntb.models.impl.UsersDAOImpl;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class UserAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        ActionForward forward = mapping.getInputForward();
        UserForm userForm = (UserForm) form;
        String action = userForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        if ("save".equalsIgnoreCase(action)) {
            Users user = new Users();
            UsersDAO userDao = new UsersDAOImpl();

            StringUtil stringUtil = new StringUtil();

            user.setUserID(userForm.getUserID());
            user.setGroupID(userForm.getGroupID());
            user.setUserName(userForm.getUserName().trim().toLowerCase());
            user.setEmail(userForm.getEmail().trim().toLowerCase());
            user.setFullName(userForm.getFullName());
            user.setGender(userForm.getGender());
            user.setPassword(stringUtil.generateMD5(userForm.getPassword()));
            user.setActive(userForm.getActive());

            if (user.getUserID() > 0) {
                Users temp = userDao.getRow(user.getUserID());
                if (temp == null) {
                    errorMessages.add("User not found!");
                } else {
                    boolean availale = true;

                    if (!temp.getUserName().equalsIgnoreCase(user.getUserName())) {
                        if (userDao.checkUsername(user.getUserName()) > 0) {
                            availale = false;
                            errorMessages.add("The username you selected is already in use.");
                        }
                    }

                    if (!temp.getEmail().equalsIgnoreCase(user.getEmail())) {
                        if (userDao.checkEmail(user.getEmail()) > 0) {
                            availale = false;
                            errorMessages.add("The email address you entered is already in use.");
                        }
                    }

                    if (availale) {
                        userDao.update(user);
                        successMessages.add("Account Information Successfully Updated.");
                        forward = mapping.findForward("userListing");
                    }
                }
            } else {
                boolean availale = true;
                if (userDao.checkUsername(user.getUserName()) > 0) {
                    availale = false;
                    errorMessages.add("The username you selected is already in use.");
                }
                if (userDao.checkEmail(user.getEmail()) > 0) {
                    availale = false;
                    errorMessages.add("The email address you entered is already in use.");
                }
                if (availale) {
                    userDao.add(user);
                    successMessages.add("Account Successfully Created.");
                    forward = mapping.findForward("userListing");
                }
            }
        } else if (userForm.getUserID() > 0) {
            UsersDAO userDao = new UsersDAOImpl();
            Users user = userDao.getRow(userForm.getUserID());

            userForm.setUserID(user.getUserID());
            userForm.setGroupID(user.getGroupID());
            userForm.setUserName(user.getUserName());
            userForm.setEmail(user.getEmail());
            userForm.setFullName(user.getFullName());
            userForm.setGender(user.getGender());
            userForm.setActive(user.getActive());
            userForm.setCreatedDate(user.getCreatedDate());

            forward = mapping.findForward("userEdit");
        } else {
            userForm.setUserID(0);
        }

        if (!"delete".equalsIgnoreCase(action)) {
            UserGroupsDAO userGroupsDao = new UserGroupsDAOImpl();
            request.setAttribute("groupList", userGroupsDao.getList());
        }

        request.setAttribute("errorMessages", errorMessages);
        request.setAttribute("successMessages", successMessages);

        return forward;
    }
}
